package cn.ycc1.boot3fastjson.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ycc
 * @date 2024/8/18
 */
@RestController
@RequestMapping("/person")
public class PersonController {
    @GetMapping("/test")
    public String test() {
        Person person = new Person();
        person.setUserName("ycc");
        person.setAge(18);
        Color color = new Color();
        color.setName("red");
        person.setColor(color);
        // JSON.toJSONString(person)更适用于将Java对象转换为JSON字符串的场景，
        // 因为它可以直接处理各种类型的Java对象，包括集合类；
        // 而JSONObject.toJSONString(person)更适合于已知的JSONObject对象转换为其字符串表达形式的情景
        String str = JSON.toJSONString(person);
        // String str = JSONObject.toJSONString(person);
        System.out.println(str);
        return str;
    }

    class Person {
        private String userName;
        private int age;

        public Color getColor() {
            return color;
        }

        public void setColor(Color color) {
            this.color = color;
        }

        private Color color;

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
        public String getUserName() {
            return userName;
        }
        public void setUserName(String userName) {
            this.userName = userName;
        }

    }

    class Color {
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        private String name;

    }
}
